Install packages
install.packages("imager", dependencies=TRUE)
install.packages("ggplot2")
load the image
library("imager")
# store image data in variable.
img <- load.image("showmewhatugot.png")
# show image via Rstudio
img
dim(img)
[1] 301 169 1 4
imgFrame <- as.data.frame(img)
head(imgFrame)
imgPivot <- reshape(imgFrame,direction="wide",timevar = "cc",idvar = c("x","y"))
head(imgPivot)
names(imgPivot)[3] <- "R"
names(imgPivot)[4] <- "G"
names(imgPivot)[5] <- "B"
head(imgPivot)
library("ggplot2")
imageColor <- rgb(imgPivot[c("R", "G", "B")])
ggplot(data = imgPivot, aes(x = x, y = y)) +
geom_point(colour = imageColor) +
coord_fixed()
kClusters <- 5
kMeansResult <- kmeans(imgPivot[c("R", "G", "B")], centers = kClusters)
kMeansResult$center
R G B
1 0.8602543 0.8895529 0.7134287
2 0.9216964 0.9469119 0.8313661
3 0.2900271 0.2551859 0.2106481
4 0.9012171 0.8197549 0.4552957
5 0.5892334 0.6252089 0.4504169
imgPivot$cluster <- kMeansResult$cluster
clusterColours = rgb(kMeansResult$center)
plot(0, type="n", axes=FALSE, ylab="", xlab="", ylim=c(2,0), xlim=c(0,5))
title("Clusters as colours")
rect(0,0,1,1,col='#ffde00')
rect(1,0,2,1,col='#1c142d')
for (i in 0:(kClusters-1)) {
rect(i,0,i+1,1,col=clusterColours[i+1])
}
text(0.5,0.5,'1')
SetTextContrastColor <- function(color)
{
ifelse( mean(col2rgb(color)) > 127, "black", "white")
}
text(0.5,0.5,'1', col=SetTextContrastColor('#FDF700'))
##
#install.packages("plotly")
library("plotly")
Loading required package: ggplot2
Attaching package: ‘plotly’
The following object is masked from ‘package:ggplot2’:
last_plot
The following object is masked from ‘package:stats’:
filter
The following object is masked from ‘package:graphics’:
layout
imgPivotSample <- imgPivot[sample(1:nrow(imgPivot), 1000, replace=FALSE),]
plot <- plot_ly(
imgPivotSample, x = ~R, y = ~G, z = ~B,
color = ~cluster, colors = clusterColours,
marker = list(symbol = "circle", size = 4)
) %>%
add_markers() %>%
layout(
scene = list(xaxis = list(title = 'Red', color='#FF0000'),
yaxis = list(title = 'Green', color='#00FF00'),
zaxis = list(title = 'Blue', color='#0000FF'))
)
plot
kClustersMax <- 10
wss <- sapply(1:kClustersMax,
function(k){kmeans(imgPivot[c("R", "G", "B")], k)$tot.withinss})
plot(1:kClustersMax, wss,
type="b", pch = 19, frame = FALSE,
xlab="Number of clusters K",
ylab="Total within-clusters sum of squares")
#Makes a vertical line at 3
abline(v = 3, lty =2)
kMeansResult$centers[imgPivot$cluster,]
R G B
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
2 0.9216964 0.9469119 0.8313661
[ reached getOption("max.print") -- omitted 50536 rows ]
kColours <- rgb(kMeansResult$centers[imgPivot$cluster,])
ggplot(data = imgPivot, aes(x = x, y = -y)) + geom_point(colour = kColours) + coord_fixed()
clusterLabel <- 2
grey_scale <- function(colour){
grey <- colour[1] * 0.2126 + colour[2] * 0.7152 + colour[3] * 0.0722
return(rgb(grey,grey,grey))
}
color_or_grey <- function(row){
if(row["cluster"]==clusterLabel){
return(rgb(row["R"],row["G"],row["B"]))
}
else{
return(grey_scale(as.numeric(row[c("R","G","B")])))
}
}
imgPivot$splash <- apply(imgPivot,1,color_or_grey)
ggplot(data = imgPivot, aes(x = x, y = -y)) + geom_point(colour = imgPivot$splash) + coord_fixed()